<template>
    <div class="background-image">
        <div id="main" style="width: 600px;height:400px;margin:0 auto;background-color: rgba(255, 255, 255, 0.9);"></div>
    </div>
</template>

<script setup>
import * as echarts from 'echarts';
import { searchStock } from '../api/api.js';
import { onMounted, ref } from 'vue';

onMounted(async () => {
    try {
        // 获取库存数据
        const res = await searchStock({
            pageNum: 1,
            pageSize: 10
        });
        console.log('库存数据:', res);
        
        if (res.data.code === 200) {
            const stockData = res.data.data.resultList;
            
            // 处理数据用于图表展示
            const goodsNames = stockData.map(item => item.goodsName);
            const stockCounts = stockData.map(item => item.count);

            // 初始化图表
            const chartDom = document.getElementById('main');
            const myChart = echarts.init(chartDom);
            const option = {
                title: {
                    text: '仓库库存统计'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                xAxis: {
                    type: 'category',
                    data: goodsNames,
                    axisLabel: {
                        interval: 0,
                        rotate: 30
                    }
                },
                yAxis: {
                    type: 'value',
                    name: '库存数量'
                },
                series: [
                    {
                        name: '库存量',
                        type: 'bar',
                        data: stockCounts,
                        itemStyle: {
                            color: '#8395a7'
                        },
                        label: {
                            show: true,
                            position: 'top'
                        }
                    }
                ]
            };

            myChart.setOption(option);
            
            // 监听窗口大小变化
            window.addEventListener('resize', () => {
                myChart.resize();
            });
        }
    } catch (error) {
        console.error('获取库存数据失败:', error);
    }
});
</script>

<style scoped>
    .background-image {
        background-image: url('../assets/BackGround.jpg');
        background-size: cover;
        background-position: center;
        height: 84vh;
    }
</style>